/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("mes_userpost",Enums.ActionPermissionOptions.Search)]
 */
using Azure.Core;
using Dm.filter;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using VolPro.Core.DbSqlSugar;
using VolPro.Core.ManageUser;
using VolPro.Core.Utilities;
using VolPro.Entity.DomainModels;
using VolPro.MES.IRepositories;
using VolPro.MES.IServices;
using VolPro.MES.Repositories;

namespace VolPro.MES.Controllers
{
    public partial class mes_userpostController
    {
        private readonly Imes_userpostService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Imes_userpostRepository _userpostRepository;

        [ActivatorUtilitiesConstructor]
        public mes_userpostController(
            Imes_userpostService service,
            IHttpContextAccessor httpContextAccessor,
            Imes_userpostRepository userpostRepository
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
            _userpostRepository = userpostRepository;
        }

        //[HttpPost, Route("mes_userpost"), AllowAnonymous]
        [HttpPost, Route("mes_userpost"), AllowAnonymous]
        public async Task<IActionResult> mes_userpost()
        {
            WebResponseContent responseContent = new WebResponseContent();
            try
            {
                // 主要方法：从 HttpContext 的 Request 头部获取
                if (HttpContext.Request.Headers.TryGetValue("Authorization", out var authHeader))
                {
                    // 提取 Bearer Token
                    var token = authHeader.ToString().Replace("Bearer ", "", StringComparison.OrdinalIgnoreCase);

                    // 现在你拿到了 token 字符串
                    // 注意：通常你不需要直接使用这个字符串，因为认证中间件已经帮你解析验证过了。
                    // 这里获取通常是为了记录日志或传递到下层服务等特殊需求。

                    // ... 你的业务逻辑 ...
                }
                int userId = UserContext.Current.UserId;
                if (userId==0)
                {
                    responseContent.Code = "300";
                    responseContent.OK("无此用户");
                }
                var t = _userpostRepository.DbContext.Queryable<mes_userpost>().Where(it => it.UserId == userId).FirstOrDefault();
                //列化，Formatting.Indented 使输出的JSON格式美观（有缩进和换行）
                string serializedJson = JsonConvert.SerializeObject(t, Formatting.Indented);
                if (t == null || string.IsNullOrEmpty(t.ProcessID.ToString()))
                {
                    responseContent.Code = "300";
                    responseContent.OK("此用户未分配工位/工序，请联系管理员", t);
                }
                else
                {
                    responseContent.Code = "200";
                    responseContent.OK("成功", t);
                }
            }
            catch (Exception ex)
            {
                responseContent.Code = "500";
                responseContent.Error(ex.Message);
            }
            return Json(responseContent);
        }
        //[HttpPost, Route("mes_userpost"), AllowAnonymous]
        //[HttpPost, Route("listBoms"), AllowAnonymous]
        //public async Task<IActionResult> listBoms()
        //{
        //    return Json(await Service.Login(loginInfo));
        //}
    }
}
